import 'package:flutter/material.dart';
import 'package:flutter_axrjc/routes.dart';
import 'package:flutter_axrjc/style.dart';
import 'package:flutter_axrjc/widget/common_widget.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'logic.dart';

class MyPostPage extends StatelessWidget {
  const MyPostPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<MyPostLogic>();
    final state = Get.find<MyPostLogic>().state;
    return Scaffold(
        backgroundColor: const Color(0xffF3F6F9),
        appBar: AppBar(
          backgroundColor: Colors.white,
          foregroundColor: Colors.black,
          titleSpacing: -10,
          elevation: 0,
          centerTitle: true,
          title: const Text(
            "我的发布",
            style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
          ),
        ),
        body: Column(
          children: [
            TabBar(
              controller: logic.tabArticle,
              onTap: (index) {
                state.currentType.value = index;
                logic.getPage(isRefresh: true);
              },
              tabs: const [
                Tab(
                    height: 40,
                    iconMargin: EdgeInsets.zero,
                    child: Text("帮助中心文章")),
                Tab(
                    height: 40,
                    iconMargin: EdgeInsets.zero,
                    child: Text("护眼知识文章"))
              ],
              indicatorSize: TabBarIndicatorSize.label,
              padding: EdgeInsets.zero,
              indicatorPadding: EdgeInsets.zero,
              indicatorWeight: 2,
              labelColor: orangeColor,
              unselectedLabelColor: Colors.black,
              indicatorColor: orangeColor,
              labelPadding: const EdgeInsets.symmetric(horizontal: 10),
              isScrollable: true,
              automaticIndicatorColorAdjustment: true,
            ),
            Expanded(
              child: SmartRefresher(
                header: reFreshHeader(),
                footer: loadFooter(),
                enablePullUp: true,
                enablePullDown: true,
                onLoading: () {
                  logic.getPage(isRefresh: false);
                },
                onRefresh: () {
                  logic.getPage(isRefresh: true);
                },
                controller: logic.refreshController,
                child: Obx(() {
                  if (state.currentType.value == 0) {
                    if (state.helpList.isEmpty) {
                      return const Padding(
                        padding: EdgeInsets.symmetric(vertical: 100),
                        child: Center(
                          child: Text("暂时没有相关内容"),
                        ),
                      );
                    }
                    return ListView.builder(
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      itemCount: state.helpList.length,
                      itemBuilder: (BuildContext context, int index) {
                        var bean = state.helpList[index];
                        return Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: InkWell(
                            onTap: () {
                              Get.toNamed(MainRoutes.helpCenterDetail,
                                  arguments: bean);
                            },
                            child: Container(
                              padding: const EdgeInsets.all(15),
                              decoration:
                                  const BoxDecoration(color: Colors.white),
                              child: Column(
                                children: [
                                  Row(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Expanded(
                                          child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            "${bean.contentTitle}",
                                            style: const TextStyle(
                                                fontSize: 14,
                                                color: Color(0xff3d3d3d)),
                                          ),
                                          const SizedBox(
                                            height: 5,
                                          ),
                                          Text(
                                            bean.contentContent ?? '',
                                            maxLines: 2,
                                            style: const TextStyle(
                                                fontSize: 12,
                                                color: Color(0xff3d3d3d)),
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                        ],
                                      )),
                                      const SizedBox(
                                        width: 10,
                                      ),
                                      ClipRRect(
                                        borderRadius: BorderRadius.circular(5),
                                        child: SizedBox(
                                          width: 80,
                                          height: 80,
                                          child: Image.network(
                                            "${bean.articelHomePic}",
                                            fit: BoxFit.cover,
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: [
                                      Padding(
                                        padding: const EdgeInsets.symmetric(
                                            horizontal: 5, vertical: 10),
                                        child: InkWell(
                                          onTap: () {
                                            Get.toNamed(MainRoutes.editHelp,
                                                arguments: bean);
                                          },
                                          child: Container(
                                            padding: const EdgeInsets.symmetric(
                                                horizontal: 15, vertical: 8),
                                            decoration: BoxDecoration(
                                                border: Border.all(
                                                  color: Colors.grey
                                                      .withAlpha(200),
                                                ),
                                                borderRadius:
                                                    BorderRadius.circular(4)),
                                            child: Row(
                                              children: [
                                                Icon(
                                                  Icons.edit_note,
                                                  color: Colors.grey.shade600,
                                                ),
                                                Text(
                                                  "修改",
                                                  style: TextStyle(
                                                      color:
                                                          Colors.grey.shade600),
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                      ),
                                      InkWell(
                                        onTap: () {
                                          logic.deleteHelp(bean);
                                        },
                                        child: Padding(
                                          padding: const EdgeInsets.symmetric(
                                              horizontal: 5, vertical: 10),
                                          child: Container(
                                            padding: const EdgeInsets.symmetric(
                                                horizontal: 15, vertical: 8),
                                            decoration: BoxDecoration(
                                                border: Border.all(
                                                  color: Colors.grey
                                                      .withAlpha(200),
                                                ),
                                                borderRadius:
                                                    BorderRadius.circular(4)),
                                            child: Row(
                                              children: [
                                                Icon(
                                                  Icons.delete_forever_outlined,
                                                  color: Colors.grey.shade600,
                                                ),
                                                Text(
                                                  "删除",
                                                  style: TextStyle(
                                                      color:
                                                          Colors.grey.shade600),
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                      ),
                                    ],
                                  )
                                ],
                              ),
                            ),
                          ),
                        );
                      },
                    );
                  } else {
                    if (state.knowledgeList.isEmpty) {
                      return const Padding(
                        padding: EdgeInsets.symmetric(vertical: 100),
                        child: Center(
                          child: Text("暂时没有相关内容"),
                        ),
                      );
                    }
                    return ListView.builder(
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      itemCount: state.knowledgeList.length,
                      itemBuilder: (BuildContext context, int index) {
                        var bean = state.knowledgeList[index];
                        return Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: InkWell(
                            onTap: () {
                              Get.toNamed(MainRoutes.knowledgeDetail,
                                  arguments: bean);
                            },
                            child: Container(
                              padding: const EdgeInsets.all(15),
                              decoration:
                                  const BoxDecoration(color: Colors.white),
                              child: Column(
                                children: [
                                  Row(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Expanded(
                                          child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            "${bean.articleTitle}",
                                            style: const TextStyle(
                                                fontSize: 14,
                                                color: Color(0xff3d3d3d)),
                                          ),
                                          const SizedBox(
                                            height: 5,
                                          ),
                                          Text(
                                            bean.articleContent ?? '',
                                            maxLines: 2,
                                            style: const TextStyle(
                                                fontSize: 12,
                                                color: Color(0xff3d3d3d)),
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                        ],
                                      )),
                                      const SizedBox(
                                        width: 10,
                                      ),
                                      ClipRRect(
                                        borderRadius: BorderRadius.circular(5),
                                        child: SizedBox(
                                          width: 80,
                                          height: 80,
                                          child: Image.network(
                                            "${bean.articelHomePic}",
                                            fit: BoxFit.cover,
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: [
                                      Padding(
                                        padding: const EdgeInsets.symmetric(
                                            horizontal: 5, vertical: 10),
                                        child: InkWell(
                                          onTap: () {
                                            Get.toNamed(MainRoutes.editArticle,
                                                arguments: bean);
                                          },
                                          child: Container(
                                            padding: const EdgeInsets.symmetric(
                                                horizontal: 15, vertical: 8),
                                            decoration: BoxDecoration(
                                                border: Border.all(
                                                  color: Colors.grey
                                                      .withAlpha(200),
                                                ),
                                                borderRadius:
                                                    BorderRadius.circular(4)),
                                            child: Row(
                                              children: [
                                                Icon(
                                                  Icons.edit_note,
                                                  color: Colors.grey.shade600,
                                                ),
                                                Text(
                                                  "修改",
                                                  style: TextStyle(
                                                      color:
                                                          Colors.grey.shade600),
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                      ),
                                      InkWell(
                                        onTap: () {
                                          logic.deleteKnowledge(bean);
                                        },
                                        child: Padding(
                                          padding: const EdgeInsets.symmetric(
                                              horizontal: 5, vertical: 10),
                                          child: Container(
                                            padding: const EdgeInsets.symmetric(
                                                horizontal: 15, vertical: 8),
                                            decoration: BoxDecoration(
                                                border: Border.all(
                                                  color: Colors.grey
                                                      .withAlpha(200),
                                                ),
                                                borderRadius:
                                                    BorderRadius.circular(4)),
                                            child: Row(
                                              children: [
                                                Icon(
                                                  Icons.delete_forever_outlined,
                                                  color: Colors.grey.shade600,
                                                ),
                                                Text(
                                                  "删除",
                                                  style: TextStyle(
                                                      color:
                                                          Colors.grey.shade600),
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                      ),
                                    ],
                                  )
                                ],
                              ),
                            ),
                          ),
                        );
                      },
                    );
                  }
                }),
              ),
            )
          ],
        ));
  }
}
